package com.loie.xcx.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loie.xcx.config.TaxCloudConfig;
import com.loie.xcx.mapper.SysAttachmentMapper;
import com.loie.xcx.model.bo.SysAttachment;
import com.loie.xcx.model.dto.AttachmentVO;
import com.loie.xcx.model.dto.IwinstanceQueryDTO;
import com.loie.xcx.service.SysAttachmentService;
import com.loie.xcx.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-03-07
 */
@Service
@Slf4j
public class SysAttachmentServiceImpl extends ServiceImpl<SysAttachmentMapper, SysAttachment> implements SysAttachmentService {
    @Autowired
    private TaxCloudConfig taxCloudConfig;

    @Override
    public
    Boolean reportdbtofile(List<String> recordlist) throws Exception {
        Boolean issuccess=false;

        for (String recordid:recordlist){
            System.out.println(recordid);
        QueryWrapper<SysAttachment> wrapper1 = new QueryWrapper<>();

        wrapper1.eq("recordid", recordid);

        List<SysAttachment> sysAttachmentList =
                baseMapper.selectList(wrapper1);

        if (sysAttachmentList.size() > 0) {
            SysAttachment sysAttachment = sysAttachmentList.get(0);

            byte[] reportbytes = sysAttachment.getData();
            if (reportbytes.length > 0) {
                //先判断原来的文件是否存在，如果存在，则删除
                if (!StringUtils.isEmpty(sysAttachment.getStoredPath())) {

                    File dest1 = new File(taxCloudConfig.getAttachmentpath() + sysAttachment.getStoredPath()
                            + sysAttachment.getRecordID() + ".iAttachment");
                    if (dest1.exists()) {
                        dest1.delete();
                    }


                }

                String datePath = DateUtil.fileformatDate(new Date());

                String filepath = recordid + ".iAttachment";
                filepath = taxCloudConfig.getAttachmentpath() + datePath + filepath;
                System.out.println(filepath);
                File dest = new File(filepath);
                //检测是否存在该目录
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }

                FileOutputStream fos = new FileOutputStream(filepath);
                fos.write(reportbytes);
                fos.close();
//                    UpdateWrapper<SysAttachment> updateWrapper = new UpdateWrapper();
//                    updateWrapper.eq("recordid", recordid);
//                    updateWrapper.set("storedPath", datePath);
                //baseMapper.update(null, updateWrapper);

                sysAttachment.setStoredPath(datePath.replace("/", "\\"));
                log.info(sysAttachment.getName()+"已保存在附件目录"+ sysAttachment.getStoredPath());
                baseMapper.update_storepath(sysAttachment);

            }


                }

            issuccess = true;
        }
        return  issuccess;
    }

    @Override
    public
    List<AttachmentVO> getAttachmentList(IwinstanceQueryDTO iwinstanceQueryDTO) {
        return baseMapper.getAttachmentList(iwinstanceQueryDTO);
    }
}
